package imaps;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.swing.JOptionPane;
import java.io.*;
import analizadorLexico.*;
import grafo.*;




public class ReadMail {
  private String from;
  private String subject;
  private String body;
  private Message message;
  private FileWriter fichero = null;
  private PrintWriter pw = null;
  private Subst s=new Subst();
  private Grafo grafo=new Grafo();
  private answerMail resp;
  
  public ReadMail(){
  
  }
  
  protected ReadMail(Message message) {
   this.message = message;
   
    try {
      InternetAddress fromAddress = (InternetAddress) message.getFrom()[0];

      from = fromAddress.getAddress();
    } catch (Exception e) {
      from = "Remitente no disponible";
    }

    try {
      subject = message.getSubject();
    } catch (Exception e) {
      subject = "Asunto no disponible";
    }
    
    
    
    try {
        if( message.isMimeType("text/plain") )
        {
            body = message.getContent().toString();
        }
        else
        {
          Multipart mp  = (Multipart)message.getContent();
          InputStream is = mp.getBodyPart(0).getInputStream();
        
          StringBuffer sb = new StringBuffer();
          byte[] b = new byte[100000];
          int noChars = is.read(b);
          sb.append(new String(b, 0, noChars));
          body = sb.toString();
        }
    }
    catch( Exception e)
    {
        body = "Mensaje no disponible";
    }
  }
  
  public void read()
  { 
	  
	  //System.err.println(subject);
	  
      try
      {
          fichero = new FileWriter("src/asunto.liam");
          pw = new PrintWriter(fichero);
          
          pw.println(subject+body);
          
          
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
         try {
         if (null != fichero)
            fichero.close();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      
  	  String [] e=new String[1];
  	  e[0]="src/asunto.liam";
  	  s.main(e);
  	      
  	 try {
		resp=new answerMail(grafo.leerCorreo(),from);
	} catch (Exception e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	
  }
  
  public ArrayList<String> readAsun(String asunt)
  { 
	  
      try
      {
    	  
          fichero = new FileWriter("src/asunto.liam");
          pw = new PrintWriter(fichero);
          
          pw.println(asunt);
          
          
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
         try {
         if (null != fichero)
            fichero.close();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      //mandamos a flex
  	  String [] e=new String[1];
  	  e[0]="src/asunto.liam";
  	  s.main(e);
  	
  	  
	return grafo.buscarAsunto();
  }
  
  
  public static List getMails(Message[] messages) {
    List list = new ArrayList();
   
    for (int i = 0; i < messages.length; i++) {
    	try{
    	if(!messages[i].isSet(Flags.Flag.FLAGGED)){
    		
    		messages[i].setFlag(Flags.Flag.FLAGGED, true);
    		list.add(new ReadMail(messages[i]));
       }
    	else{
    		list.add("nada");
    	}
       }catch (Exception e1) {
   		
   		e1.printStackTrace();
   	}
    }
    
    return list;
  }
  
  public void readBody()
  {
      try {
          JOptionPane.showMessageDialog(null,this.body, "Asunto: " + this.subject, JOptionPane.PLAIN_MESSAGE);
      }
      catch( Exception e )
      {
          System.out.println("Error al mostrar el mensaje");
      }   
  }

public String getSubject() {
	return subject;
}

public void setSubject(String subject) {
	this.subject = subject;
}

public String getBody() {
	return body;
}

public void setBody(String body) {
	this.body = body;
}

public String getFrom() {
	return from;
}

public void setFrom(String from) {
	this.from = from;
}
}